package com.jinyijia.api.rest.v1;

import com.jinyijia.api.infrastructure.FinanceManager;
import com.jinyijia.api.infrastructure.UserInfoManager;
import com.jinyijia.api.rest.v1.finance.dto.FinanceDTO;
import com.jinyijia.api.rest.v1.finance.vo.FinanceDetailVO;
import com.jinyijia.api.rest.v1.finance.vo.FinancesVO;
import com.jinyijia.api.rest.v1.order.vo.FinanceOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhw
 * @date 2018/8/8
 **/
@RestController
@RequestMapping("finance")
public class FinanceController {

    @Autowired
    private FinanceManager financeManager;

    /**
     * 待开票订单
     */
    @GetMapping("orders")
    public Map getOrder() {
        List<FinanceOrderVO> financeOrderVOS = financeManager.financeOrders(UserInfoManager.getCurrentUserId());
        Map map = new HashMap();
        map.put("code", 0);
        map.put("message", "OK");
        map.put("credit", financeOrderVOS.stream().mapToDouble(financeOrderVO -> financeOrderVO.getActually_total_price().doubleValue()).sum());
        map.put("order", financeOrderVOS);
        return map;
    }

    /**
     * 已开票列表
     */
    @GetMapping("history")
    public FinancesVO getHistory() {
        Integer customId = UserInfoManager.getCurrentUserId();
        return new FinancesVO(0, "OK", financeManager.credit(customId), financeManager.plain(customId), financeManager.vat(customId), financeManager.history(customId), financeManager.getHistory(customId));
    }

    /**
     * 开票申请
     */
    @PostMapping
    public Object save(@RequestBody FinanceDTO financeDTO) {
        return financeManager.save(UserInfoManager.getCurrentUserId(), financeDTO);
    }


    /**
     * 开票详情
     */
    @GetMapping("/orders/{code}")
    public FinanceDetailVO get(@PathVariable String code,@RequestParam String applyTime) {
        return financeManager.get(code,applyTime);
    }


}
